﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 码云小公举
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.DealWithDi(new DirectoryInfo("D:\\2017项目\\LBCMS\\LBCMS\\admin"));
            this.DealWithEmptyDi();
        }

        List<string> lstObjPath = new List<string>();
        List<DirectoryInfo> lstDi = new List<DirectoryInfo>();
        private void btnPath_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择Code所在文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.DealWithDi(new DirectoryInfo(dialog.SelectedPath));
                this.DealWithEmptyDi();
                //this.DealWithObjDi();
            }
        }


        private void DealWithDi(DirectoryInfo objDi)
        {
            DirectoryInfo[] subDi = objDi.GetDirectories();
            foreach (var item in subDi)
            {
                if (ckDelBin.Checked
                    && (item.Name == "obj" || item.Name == "bin"))
                {
                    lstObjPath.Add(item.FullName);
                    continue;
                }
                if (item.Name != ".vs" && item.Name != "packages")
                {
                    lstDi.Add(item);
                    DealWithDi(item);
                }
                
            }
        }
        private void DealWithEmptyDi()
        {
            foreach (var item in lstDi)
            {
                if (Directory.GetDirectories(item.FullName).Length == 0 && Directory.GetFiles(item.FullName).Length == 0)
                {
                    FileStream fs1 = new FileStream(item.FullName + "/ignore.keep", FileMode.Create);
                    fs1.Close();
                }
            }
        }

        private void DealWithObjDi()
        {
            foreach (var item in lstObjPath)
            {
                Directory.Delete(item, true);
            }
        }
    }
}
